
import { GCtrl } from "../../lib/util/GCtrl";
import headItem from "./headItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class DWC_ChangeHead extends cc.Component {

    @property(cc.Prefab)
    headItemPrf: cc.Prefab = null;

    @property(cc.Node)
    content: cc.Node = null;
    @property(cc.Node)
    pickCircle: cc.Node = null;

    @property(cc.Sprite)
    img_head: cc.Sprite = null;
    /** 性别选项数组 [man,woman] */
    @property(cc.Toggle)
    tglArr: cc.Toggle[] = [];

    // LIFE-CYCLE CALLBACKS:    
    private myInfo = null;
    private pickEventName: string = "pickHead";
    private curHeadId: string = null;
    private itemIdx: number = null;
    private curSex: number = null;

    onEnable() {
        this.node.on(this.pickEventName, this.pickHead, this);
    }
    onDisable() {
        this.node.off(this.pickEventName, this.pickHead, this);
    }
    init(args) {
        this.myInfo = GCtrl.session.getPlayer();
        this.curHeadId = this.myInfo.logo;
        this.curSex = this.myInfo.sex || 1;
        this.setHead(this.curHeadId);
        this.setSexChoose(this.curSex);
        this.setHeadList(this.curSex);
    }
    /** 点击头像 */
    private pickHead(_evt: {
        head: cc.Node,
        headId: string,
        itemIdx: number
    }) {
        if (!_evt.head) return false;
        this.pickCircle.parent = _evt.head;
        this.pickCircle.active = true;
        this.pickCircle.position = cc.v2(0, 0);
        this.curHeadId = _evt.headId;
        this.itemIdx = _evt.itemIdx;
        this.setHead(this.curHeadId);
        return true;
    }
    /** 玩家性别选择 */
    sexChange(_evt): void {
        switch (_evt.node.name) {
            case `tgl_man`:
                this.curSex = 1;
                break;
            case `tgl_woman`:
                this.curSex = 2;
                break;
        }
        if (this.curSex != 1 && this.curSex != 2) return;
        if (this.itemIdx >= 0) {
            this.setHeadList(this.curSex);
            //设置当前headid
            let _node: cc.Node = this.content.children[this.itemIdx];
            if (_node && _node.active) {
                let _item = _node.getComponent(_node.name);
                if (_item && _item[`headId`]) {
                    this.curHeadId = _item[`headId`];
                    this.setHead(this.curHeadId);
                }
            }
        }
    }
    /** 设置性别选中 
     * @param _sex 性别 1男 2女
    */
    setSexChoose(_sex: number): void {
        if (!_sex || _sex < 0) return;
        if (_sex > 2) _sex = 2;
        this.tglArr[_sex - 1].isChecked = true;
    }
    onClickBack() {
        GCtrl.UIMgr.close(GCtrl.GLConfig.DWC_ChangeHead_path, GCtrl.UIMgr.closeType.hide);
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
    }
    onClickConfirmChange() {
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
        if (!this.curHeadId) {
            GCtrl.GLUtils.showTips("请选择头像!");
            return;
        }
        //头像框，现在游戏没有使用，写死一个
        let _frame: number = 1;
        let msg = {
            logo: `${this.curHeadId}`,
            frame: _frame,
        }
        GCtrl.GLUtils.sendRequest("ChangeLogoAndframe", msg, function (error, data) {
            cc.vv.UIMgr.close(cc.vv.GLConfig.DWC_ChangeHead_path, cc.vv.UIMgr.closeType.destory);
            if (data.errcodedes) {
                GCtrl.GLUtils.showTips(data.errcodedes);
                return;
            }
            GCtrl.session.setLogo(this.curHeadId);
            GCtrl.session.setFrame(_frame);
            GCtrl.eventCustom.emit("changeHeadSuccess", this.curHeadId);
            GCtrl.eventCustom.emit("ChangeHeadEnd");
            GCtrl.GLUtils.showTips("更改成功！");
        }.bind(this));
        //性别 change by tm
        if (this.myInfo.sex != this.curSex) {
            GCtrl.GLUtils.sendRequest(`ChangeSex`, { sex: this.curSex }, function (_err, _data) {
                if (_data.errcodedes) {
                    GCtrl.GLUtils.showTips(_data.errcodedes);
                    return;
                }
                GCtrl.session.setSex(this.curSex);
            }.bind(this));
        }
    }
    /** 设置头像 */
    setHead(_head: string): void {
        GCtrl.setHeadSpr(_head, this.img_head);
        this.img_head.node.height = 88;
        this.img_head.node.width = 88;
    }
    /** 设置头像列表 
     * @param _sex 性别 1男 2女
    */
    setHeadList(_sex: number): void {
        if (!_sex) {
            _sex == 0 && this.setHead(`0`);
            return;
        }
        //设置头像列表
        let _ids: string[] = this.getHeadIds(_sex);
        _ids.forEach((_v, _idx) => {
            let headItem = this.content.children[_idx];
            headItem || (headItem = cc.instantiate(this.headItemPrf));
            headItem.parent = this.content;
            headItem.active = true;
            let headItem_ts: headItem = headItem.getComponent(headItem.name);
            headItem_ts.init({
                pChangeHead: this,
                pickEventName: this.pickEventName,
                id: _v,
                idx: _idx
            });
        });
        //多余的item隐藏
        for (let j = _ids.length; j < this.content.children.length; ++j) {
            this.content.children[j].active = false;
        }
    }
    /** 获得头像id数组 
     * @param _sex 性别 1男 2女 (<0 时，全部头像)
    */
    getHeadIds(_sex: number): string[] {
        let _ids: string[] = [];
        if (!_sex) return _ids;
        if (_sex > 0) {
            switch (_sex) {
                case 1:
                    for (let _i = GCtrl.dataConfig.MIN_HEAD_ID + 1; _i <= GCtrl.dataConfig.MAX_HEAD_ID; _i++) {
                        _i > GCtrl.dataConfig.SEX_DIF_HEAD_ID && _ids.push(`${_i}`);
                    }
                    break;
                case 2:
                    for (let _i = GCtrl.dataConfig.MIN_HEAD_ID + 1; _i <= GCtrl.dataConfig.MAX_HEAD_ID; _i++) {
                        _i <= GCtrl.dataConfig.SEX_DIF_HEAD_ID && _ids.push(`${_i}`);
                    }
                    break;
            }
        } else {
            for (let _i = GCtrl.dataConfig.MIN_HEAD_ID + 1; _i <= GCtrl.dataConfig.MAX_HEAD_ID; _i++) {
                _ids.push(`${_i}`);
            }
        }
        return _ids;
    }
}
